var flip={
  chapters_data:[],
  current_index:0,//当前数组内的索引
  current_page:1,//当前页
  chapter_num:0,
  page_num:0,//总页数
  per_num:10,
  start_page:0,
  isFirst:true,
  lastChapter_id:null,
  getChapterByPage:function(pagenum){
    //$(".chapter-loading").show();
    var that=this;
    $.ajax({
      url:"/index/chapter/catalog",
      type: 'GET',
      data: { bid: book_id, page:pagenum, order:order},
      dataType: 'json',
      success:function(result){
        //$(".chapter-loading").hide();
        if(result.code==1){
          var datas = result.data;
          var total = datas.chapter_num;
          //that.lastChapter_id=datas.lastChapter.cid;
          //获取一共有多少页
          var totalNum = datas.total_page;
          var currentNum = datas.page;
          that.current_index=0;
          that.chapters_data=datas.chapters;
          that.page_num=totalNum;
          that.chapter_num=total;
          that.current_page=currentNum;//当前的页码位置
          that.start_page=currentNum;//设置当前数据第一页的位置
          //第一次请求，生成翻页下拉
          var option_list="";
          if(that.isFirst){
            for(var i=1; i<=totalNum; i++){
              option_list+='<option value="'+i+'">第'+i+'页/'+totalNum+'页</option>';
            }
            $("#page-select").html($(option_list));
            
            $(".catalog-header  .title").text('目录（共'+total+'章）');
            that.isFirst=false;
          }

          that.renderList(0);
        }
      },
      error:function(){
        //$(".chapter-loading").hide();
      }
    });
  },
  getCurrentDataPage:function(){
    return Math.ceil(this.chapters_data.length/this.per_num);
  },
  renderList:function(index){
    //这里的索引是相当对于数组里面的，从0开始
    var page_str="";
    var start=index * this.per_num;
    var end=start+this.per_num;
    if(end > this.chapters_data.length){
      end=this.chapters_data.length;
    }
    var list_str="";
    var book_id = $('#info_list').attr('bid');
    for(var i=start; i<end; i++){
      var c=this.chapters_data[i];
      list_str+='<a href="/chapter/read/'+book_id+'/'+c.chapter_id+'" title="'+c.chaptername+'" >';
      if(c.is_vip==1)
      {
        list_str+='<em>VIP</em>';
      }
      list_str+= c.chaptername+'</a>';
    }

    $("#book-list").html($(list_str));
    if(this.current_page >1){
      $("#prev-page").removeAttr("disabled");
      $("#pre-class").attr("class","");
    }else{
      $("#prev-page").attr("disabled","disabled");
      $("#pre-class").attr("class","one_zhang");
    }

    if(this.current_page < this.page_num){
      $("#next-page").removeAttr("disabled");
      $("#next-class").attr("class","");
    }else{
      $("#next-page").attr("disabled","disabled");
      $("#next-class").attr("class","one_zhang");
    }
    document.getElementById("page-select").selectedIndex=(this.current_page-1);
  },
  showPage:function(pagenum){
    if(pagenum>=this.start_page && pagenum<=this.getCurrentDataPage()){//已经下载过就在本地查看
      this.current_page=pagenum;
      var index=pagenum-this.start_page;
      this.renderList(index);
    }else{
      this.current_page=pagenum;
      this.getChapterByPage(pagenum);
    }
  },
  nextPage:function(){
    //判断当前的数组是不是再范围内
    if(this.current_index < this.getCurrentDataPage()-1 ){
      //获取页面的
      this.current_index++;
      this.current_page++;
      this.renderList(this.current_index);
    }else{
      if( this.current_page<this.page_num ){
        this.current_page++;
        this.getChapterByPage(this.current_page);
      }else{
        console.log("已经是最后一页了");
      }
    }
  },
  prevPage:function(){
    //判断当前的数组是不是再范围内
    if(this.current_index > 0 ){
      //获取页面的
      this.current_index--;
      this.current_page--;
      this.renderList(this.current_index);
    }else{
      if( this.current_page > 1 ){
        this.current_page--;
        this.getChapterByPage(this.current_page);
      }else{
        console.log("已经是第一页了");
      }
    }
  },
  init:function(){
    that=this;
    var tap='ontouchstart' in window ? "tap" : "click";
    $("#next-page").bind(tap, function(){
      that.nextPage();
    })
    $("#prev-page").bind(tap, function(){
      that.prevPage();
    })
    $("#page-select").bind("change", function(e)
    {
      var page=Number(e.target.value);
      that.showPage(page);
    });
    $("#read_fx").bind(tap, function(){
      if(order=="asc"){
        order="desc";
        $("#read_fx").attr("class","read_atz");
		$("#read_fx p,#read_fx span").hide();
      }else{
        order="asc"
        $("#read_fx").attr("class","read_zta");
		$("#read_fx p,#read_fx span").hide();
      }
      that.getChapterByPage(1);
    });
  }
}